<?php
/**
 * 商品 api 接口
 * @author maniac
 */
namespace app\api\controller\v1;

use app\common\model\GoodCategory as GoodCategoryModel;
use app\facade\Evaluates as EvaluatesFacade;
use app\facade\Goods AS GoodsFacade;
use app\facade\GoodCategory AS GoodCategoryFacade;
use app\facade\GoodsCollect;
use app\common\model\Goods as GoodsModel;
use app\common\model\Buycar as BuycarModel;
use app\common\model\GoodsCollect as GoodsCollectModel;
use think\Db;
use app\common\model\ActivityGoods;
class Goods extends Base
{


    //选择分类获取商品
    public function choiceCategory(){
        $limit = input('limit');
        $page  = input('page');
        $category_id = input('category_id');
        $data = GoodsModel::limit( ($page - 1)*$limit,$limit)->where('category_id',$category_id)
            ->field('id,goods_name,goods_thumb_id,sale_price')->order('list_order desc,sale_price asc')->append(['image_uri'])->select();
        return j(0,'成功',[
            'data' => $data,
        ]);
    }
    
    /**
     * 商品详情
     */
    public function detail()
    {
        $post = input('');
//        $userToken = input('userToken/s', '');

        if (!$post['goods_id']) {
            return j(1, '请选择商品');
        }


        $where  = [
            'id' => $post['goods_id'],
        ];
        $info = GoodsFacade::goods($where)[0];

        $info['goods_detail'] = str_replace('src="/static/', 'src="'.config('system.attach_uri_prefix').'static/', $info['goods_detail']);

        if ($info) {
            return j(0, '获取商品详情成功', $info);
        }
        return j(1, '获取商品信息失败');
    }
    public function goodsList(){
        $type = input('type');//类型 1热销 2推荐
        $limit = input('limit');
        $page  = input('page');
        $keywords = input('keywords');
        $data = new \app\common\model\Goods();
        if ($type == 1){
            $data = $data->where("is_hot_sale",1);
        }else if($type == 2){
            $data = $data->where("is_recommand",1);
        }
        if (!$type){
            $where = [
                ['goods_name',"like","%".$keywords."%"],
                ['goods_type',"<>",3],
            ];
            $where1 = [
                ['goods_keywords',"like","%".$keywords."%"],
                ['goods_type',"<>",3],
            ];
            $data = $data->whereOr([$where,$where1]);
        }
        $data = $data->where("is_sale",1)->where("goods_type","<>",3)->limit(($page-1)*$limit.','.$limit)
            ->field('id,goods_name,sale_price,actual_sale_nums,goods_thumb_id')->order('id DESC')->append(['image_uri'])->select();


        return j(0, '获取商品列表成功', $data);

    }
    //收藏商品列表
    public function collectList(){
        $limit = input('limit');
        $page  = input('page');
        $user_id = $this->checkUserToken();
        $data = GoodsCollectModel::where("userid",$user_id)->append(['goods'])->limit(($page-1)*$limit,$limit)->select();
        return j(0,'成功',[
            'data' => $data
        ]);
    }
    //添加收藏商品
    public function addCollect(){
        $user_id = $this->checkUserToken();
//        $user_id = input('user_id');
        $good_id = input('good_id');
        if (empty($good_id)){
            return j(1,'商品id不能为空');
        }
        $good = GoodsCollectModel::where("userid",$user_id)->where('goods_id',$good_id)->field('id')->find();
        if (!empty($good)){
            if (GoodsCollectModel::destroy($good['id'])){
                return j(0,'取消商品收藏成功');
            }else{
                return j(1,'取消商品收藏失败');
            }
        }
        $data = new GoodsCollectModel();
        $data->userid = $user_id;
        $data->goods_id = $good_id;
        if ($data->save()){
            return j(0,'收藏成功');
        }else{
            return j(1,'收藏失败');
        }
    }
    //取消收藏商品
    public function deleteCollect(){
        $this->checkUserToken();
        $collect_id = input('collect_id');
        if (GoodsCollectModel::destroy($collect_id)){
            return j(0,'取消商品收藏成功');
        }else{
            return j(1,'取消商品收藏失败');
        }
    }
    /**
     * [取消]收藏商品
     */
    public function collect()
    {
        $userId  = $this->checkUserToken();
        $goodsId = input('post.goodsId/d', 0);
        if (!$userId) {
            exception('请先登录');
        }
        if (!$goodsId) {
            exception('请选择要收藏的商品');
        }
        try {
            $result = GoodsCollect::collect($userId, $goodsId);
            return j(0, $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
    //测试添加商品
    public function int_goods(){
        $list = Db::name('goods')->where('id',17)->find();

        unset($list['id']);
        $list_sku = Db::name('good_sku')->where('id',58)->find();
        unset($list_sku['id']);
        for ($i=0;$i<201;$i++){
            $g_id = \app\common\model\Goods::create($list);
            if($g_id->id){
               $list_sku['goods_id'] = $g_id->id;
               \app\common\model\GoodSku::create($list_sku);
            }else{
                return j(1,'shibai');
            }
            $i++;
        }
        return j(0,'chenggong');
    }

}
